//116. 填充每个节点的下一个右侧节点指针

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
    Node* connect(Node* root) 
    {
        queue<Node*> que;
        if(root)
        que.push(root);
        while(!que.empty())
        {
            int size=que.size();
            Node* tmp=que.front();
            for(int i=0;i<size;i++)
            {
                Node* n=que.front();
                if(i>0)
                {
                    tmp->next=n;
                    tmp=tmp->next;
                }
                que.pop();
                if(n->left)
                {
                    que.push(n->left);
                }
                if(n->right)
                {
                    que.push(n->right);
                }
            }
        }
        return root;
        
    }
};

//515. 在每个树行中找最大值

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) 
    {
        queue<TreeNode*> que;
        if(root)
        que.push(root);
        vector<int> ans;
        while(!que.empty())
        {
            TreeNode* n=que.front();
            int max=n->val;
            int size=que.size();
            for(int i=0;i<size;i++)
            {
                n=que.front();
                if(n->left)
                {
                    que.push(n->left);
                }
                if(n->right)
                {
                    que.push(n->right);
                }
                if(n->val>max)
                {
                    max=n->val;
                }
                que.pop();
            }
            ans.push_back(max);
        }
        return ans;
    }
};

//429. N 叉树的层序遍历

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) 
    {
        vector<vector<int>> ans;
        queue<Node*> que;
        if(root)
        {
            que.push(root);
        }
        while(!que.empty())
        {
            vector<int> tmp;
            int size=que.size();
            for(int i=0;i<size;i++)
            {
                Node* n=que.front();
                que.pop();
                tmp.push_back(n->val);
                for(int j=0;j<n->children.size();j++)
                {
                    que.push(n->children[j]);
                }


            }
            ans.push_back(tmp);
        }
        return ans;

        
    }
};

//117. 填充每个节点的下一个右侧节点指针 II




/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
    Node* connect(Node* root) 
    {
         queue<Node*> que;
        if(root)
        que.push(root);
        while(!que.empty())
        {
            int size=que.size();
            Node* tmp=que.front();
            for(int i=0;i<size;i++)
            {
                Node* n=que.front();
                if(i>0)
                {
                    tmp->next=n;
                    tmp=tmp->next;
                }
                que.pop();
                if(n->left)
                {
                    que.push(n->left);
                }
                if(n->right)
                {
                    que.push(n->right);
                }
            }
        }
        return root;
        
    }
};
